package com.vm.osm.business.service.omp.impl;

import com.vm.osm.business.dto.omp.type_dicts.TypeDict;
import com.vm.osm.business.service.omp.IOmpTypeDictsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 字典服务实现类
 *
 * @author Xuerong Xue
 */
@Service
public class OmpTypeDictsServiceImpl implements IOmpTypeDictsService {
    private static final Logger LOG = LoggerFactory.getLogger(OmpTypeDictsServiceImpl.class);
    @Value("${omp_api_url}")
    private String OMP_API_URL;
    @Autowired
    private RestTemplate restTemplate;


    @Override
    public List<TypeDict> getTopsByType(Byte type) {
        try {
            LOG.info("【字典】（根据类型获取第一级列表）type={}", type);
            if (null == type) return Collections.emptyList();
            String URL = OMP_API_URL + "/omp/v1.0/type_dicts/" + type + "/get_tops";
            ResponseEntity<TypeDict[]> entity = restTemplate.getForEntity(URL, TypeDict[].class);
            return Arrays.asList(entity.getBody());
        } catch (Exception e) {
            LOG.error("【字典】（根据类型获取第一级列表）异常", e);
            return null;
        }
    }
}
